<?php

namespace App\Admin\Controllers\Common;

use App\Models\Custom\Custom as ModelCustom;
use App\Models\Manger\Manger;
use App\Models\Settle\SettleCompany;
use App\Models\Steel\SteelArchive;
use App\Models\Steel\SteelAttrList;
use App\Models\Steel\SteelAttrType;
use App\Services\Manger\MangerAccount;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Models\Admin\AdminUser;
use App\Models\CompanyinfoDinding;
use App\Models\CompanyUserDinding;
use App\Supply\Validate\Manger\UserPost;

class CommonController extends Controller
{
    /**
     * 获取超管菜单列表
     */
    public function getmenulist(Request $request)
    {

        $menulist = [
            [
                'name' => 'superadminsite',
                'path' => '/superadminsite',
                'hidden' => false,
                'component' => 'Layout',
                'alwaysShow' => true,
                'meta' => ["title" => "账套管理", "icon" => "user", "noCache" => false, "link" => null],
                'children' => [
                    [
                        'name' => 'allacset',
                        'path' => 'allacset',
                        'hidden' => false,
                        'component' => '/superadmin/acset/allacset/index',
                        'alwaysShow' => true,
                        'meta' => ["title" => "总帐套", "icon" => "user", "noCache" => false, "link" => null],
                    ],
                    [
                        'name' => 'subacset',
                        'path' => 'subacset',
                        'hidden' => false,
                        'component' => '/superadmin/acset/subacset/index',
                        'alwaysShow' => true,
                        'meta' => ["title" => "子帐套", "icon" => "user", "noCache" => false, "link" => null],
                    ]
                ]
            ],
            [
                'name' => 'superadminmanger',
                'path' => '/superadminmanger',
                'hidden' => false,
                'component' => 'Layout',
                'alwaysShow' => true,
                'meta' => ["title" => "分配管理员", "icon" => "user", "noCache" => false, "link" => null],
                'children' => [
                    [
                        'name' => 'asignadmin',
                        'path' => 'asignadmin',
                        'hidden' => false,
                        'component' => '/superadmin/asignadmin/index',
                        'alwaysShow' => true,
                        'meta' => ["title" => "分配管理员", "icon" => "user", "noCache" => false, "link" => null],
                    ]
                ]
            ],
            [
                "path" => "/datamanage",
                "name" => "datamanage",
                "component" => "Layout",
                "hidden" => false,
                "alwaysShow" => true,
                "meta" => [
                    "title" => "数据管理",
                    "icon" => "user"
                ],
                "children" => [
                    [
                        "path" => "yoy",
                        "hidden" => false,
                        "alwaysShow" => true,
                        "component" => "/product/statefrom/yoy",
                        "name" => "yoy",
                        "meta" => [
                            "title" => "同比",
                            "icon" => "dashboard"
                        ]
                    ],
                    [
                        "path" => "qoq",
                        "hidden" => false,
                        "alwaysShow" => true,
                        "component" => "/product/statefrom/qoq",
                        "name" => "qoq",
                        "meta" => [
                            "title" => "环比",
                            "icon" => "dashboard"
                        ]
                    ],
                    [
                        "path" => "price-trend",
                        "hidden" => false,
                        "alwaysShow" => true,
                        "component" => "/product/statefrom/price-trend",
                        "name" => "price-trend",
                        "meta" => [
                            "title" => "价格走势",
                            "icon" => "dashboard"
                        ]
                    ],
                    [
                        "path" => "procurement-overview",
                        "hidden" => false,
                        "alwaysShow" => true,
                        "component" => "/product/statefrom/procurement-overview",
                        "name" => "procurement-overview",
                        "meta" => [
                            "title" => "采购概况",
                            "icon" => "dashboard"
                        ]
                    ],
                    [
                        "path" => "sales-overview",
                        "hidden" => false,
                        "alwaysShow" => true,
                        "component" => "/product/statefrom/sales-overview",
                        "name" => "sales-overview",
                        "meta" => [
                            "title" => "销售概况",
                            "icon" => "dashboard"
                        ]
                    ],
                    [
                        "path" => "fromgross",
                        "hidden" => false,
                        "alwaysShow" => true,
                        "component" => "/product/statefrom/gross",
                        "name" => "fromgross",
                        "meta" => [
                            "title" => "毛利分析表",
                            "icon" => "dashboard"
                        ]
                    ],
                    [
                        "path" => "inventory-from",
                        "component" => "ParentView",
                        "name" => "inventory-from",
                        "hidden" => false,
                        "alwaysShow" => true,
                        "meta" => [
                            "title" => "库存分析",
                            "icon" => "dashboard"
                        ],
                        "children" => [
                            [
                                "path" => "inventory-from1",
                                "hidden" => false,
                                "alwaysShow" => true,
                                "component" => "/product/statefrom/inventory-from1",
                                "name" => "inventory-from1",
                                "meta" => [
                                    "title" => "库存周转率",
                                    "icon" => "dashboard"
                                ]
                            ],
                            [
                                "path" => "inventory-from2",
                                "hidden" => false,
                                "alwaysShow" => true,
                                "component" => "/product/statefrom/inventory-from2",
                                "name" => "inventory-from2",
                                "meta" => [
                                    "title" => "库存分析",
                                    "icon" => "dashboard"
                                ]
                            ],
                            [
                                "path" => "inventory-from3",
                                "hidden" => false,
                                "alwaysShow" => true,
                                "component" => "/product/statefrom/inventory-from3",
                                "name" => "inventory-from3",
                                "meta" => [
                                    "title" => "呆滞存货分析表",
                                    "icon" => "dashboard"
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];


        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $menulist]);
        // $menulist = [
        //     [
        //         'name' => 'superadminsite',
        //         'path' => '/superadminsite',
        //         'hidden' => false,
        //         'component' => 'Layout',
        //         'alwaysShow' => true,
        //         'meta' => ["title" => "账套管理", "icon" => "user", "noCache" => false, "link" => null],
        //         'children' => [
        //             [
        //                 'name' => 'allacset',
        //                 'path' => 'allacset',
        //                 'hidden' => false,
        //                 'component' => '/superadmin/acset/allacset/index',
        //                 'alwaysShow' => true,
        //                 'meta' => ["title" => "总帐套", "icon" => "user", "noCache" => false, "link" => null],
        //             ],
        //             [
        //                 'name' => 'subacset',
        //                 'path' => 'subacset',
        //                 'hidden' => false,
        //                 'component' => '/superadmin/acset/subacset/index',
        //                 'alwaysShow' => true,
        //                 'meta' => ["title" => "子帐套", "icon" => "user", "noCache" => false, "link" => null],
        //             ]
        //         ]
        //     ],
        //     [
        //         'name' => 'superadminmanger',
        //         'path' => '/superadminmanger',
        //         'hidden' => false,
        //         'component' => 'Layout',
        //         'alwaysShow' => true,
        //         'meta' => ["title" => "分配管理员", "icon" => "user", "noCache" => false, "link" => null],
        //         'children' => [
        //             [
        //                 'name' => 'asignadmin',
        //                 'path' => 'asignadmin',
        //                 'hidden' => false,
        //                 'component' => '/superadmin/asignadmin/index',
        //                 'alwaysShow' => true,
        //                 'meta' => ["title" => "分配管理员", "icon" => "user", "noCache" => false, "link" => null],
        //             ]
        //         ]
        //     ],
        //     [
        //         'name' => 'superadmindata',
        //         'path' => '/superadmindata',
        //         'hidden' => false,
        //         'component' => 'Layout',
        //         'alwaysShow' => true,
        //         'meta' => ["title" => "数据管理", "icon" => "dashboard", "noCache" => false, "link" => null],
        //         'children' => [
        //             [
        //                 'name' => 'datamessage',
        //                 'path' => 'datamessage',
        //                 'hidden' => false,
        //                 'component' => '/superadmin/datamessage/index',
        //                 'alwaysShow' => true,
        //                 'meta' => ["title" => "数据管理", "icon" => "dashboard", "noCache" => false, "link" => null],
        //             ]
        //         ]
        //     ]
        // ];
        // $menulist=[
        //     [
        //         'menuname'=>'账套管理',
        //         'menu_type'=>'1',
        //         'route_path'=>'',
        //         'icon_str'=>'',
        //         'submenu'=>[
        //             [
        //                 'menuname'=>'总账套管理',
        //                 'menu_type'=>'2',
        //                 'route_path'=>'',
        //                 'icon_str'=>'',
        //                 'buttonlist'=>[
        //                     '新增','修改','冻结','解冻'
        //                 ]
        //             ],
        //             [
        //                 'menuname'=>'子账套管理',
        //                 'menu_type'=>'2',
        //                 'route_path'=>'',
        //                 'icon_str'=>'',
        //                 'buttonlist'=>[
        //                     '新增','修改','冻结','解冻'
        //                 ]
        //             ],

        //         ]
        //     ],
        //     [
        //         'menuname'=>'分配管理员',
        //         'menu_type'=>'1',
        //         'route_path'=>'',
        //         'icon_str'=>'',
        //         'buttonlist'=>[
        //             '新增','修改','冻结','解冻'
        //         ]
        //     ],
        //     [
        //         'menuname'=>'数据管理',
        //         'menu_type'=>'1',
        //         'route_path'=>'',
        //         'icon_str'=>'',
        //         'buttonlist'=>[

        //         ]
        //     ]
        // ];

        // return response()->json(['code'=>'000','msg'=>'操作成功','data'=>$menulist]);
    }

    /**
     * 获取管理员账号信息
     */
    public function getadminaccount()
    {
        $list = AdminUser::pluck('username');
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['list' => $list]]);
    }




    public function getcpdlist(Request $request)
    {
        $list = CompanyinfoDinding::get()->toArray();
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['list' => $list]]);
    }

    public function getcpduserlist(Request $request)
    {
        $cpd_id = intval($request->cpd_id);

        $list = CompanyUserDinding::where(['cpd_id' => $cpd_id])->get()->toArray();
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['list' => $list]]);
    }

    public function getcustomlist(Request $request)
    {
        $cpid = intval($request->cpid) ?? 0;

        $memberinfo = [];
        $memberinfo['cpid'] = $cpid;
        $isgetall = $request->isgetall ?? 0;
        $list = ModelCustom::getList($request, $memberinfo, $isgetall);

        foreach ($list['list'] as $k => $v) {
            if ($request->type == 3) {
                if ($v['total_transaction_tonnage'] > 0) {
                    $list['list'][$k]['total_transaction_tonnage'] = $v['total_transaction_tonnage'] / 10000;
                }
            }
            if (is_numeric($v['settlement_company'])) {
                $settlement_company = SettleCompany::where([['id', (int)$v['settlement_company']]])->first();
                if ($settlement_company) {
                    $settlement_company = $settlement_company->toArray();
                    $list['list'][$k]['settlement_company'] = $settlement_company['settle_company_name'];
                } else {
                    $list['list'][$k]['settlement_company'] = '';
                }
            } else {
                $list['list'][$k]['settlement_company'] = $v['settlement_company'];
            }

            if (!empty($v['manger_id'])) {
                $manger = Manger::where(['id' => $v['manger_id']])->first();
                if ($manger) {
                    $manger = $manger->toArray();
                    $list['list'][$k]['manger_name_new'] = $manger['realname'];
                } else {
                    $list['list'][$k]['manger_name_new'] = '';
                }
            } else {
                $list['list'][$k]['manger_name_new'] = '';
            }

            //业务员+往来单位名称+结算公司 = T+往来单位名称
            if($list['list'][$k]['settlement_company']){
                $list['list'][$k]['tplus_company_name'] = $list['list'][$k]['manger_name_new'].'-'.$list['list'][$k]['company_name'].'*'.$list['list'][$k]['settlement_company'];
            }
            /***
             *  增加当前数据审核状态
             *   1. 客户状态，1.待审核，2审核中，3通过  4.驳回 custom_status
             *   2. 开票状态，1.待审核，2审核中，3通过  4.驳回 invoice_status
             *
             */
            if($v['custom_status'] == '1' && $v['invoice_status'] == '1'){ //初始文件导入 状态1
                $list['list'][$k]['merge_status'] = '4';//简化权限按钮判断，1，4权限一样
                $list['list'][$k]['merge_status_desc'] = '待审核';
            }
//            if($v['custom_status'] == '2' && $v['invoice_status'] == '2'){ //驳回/待审核
//                $list['list'][$k]['merge_status'] = '2';
//                $list['list'][$k]['merge_status_desc'] = '审核中';
//            }
            if(($v['custom_status'] == '2' && $v['invoice_status'] == '3') ||
                ($v['custom_status'] == '3' && $v['invoice_status'] == '2') ||
                ($v['custom_status'] == '2' && $v['invoice_status'] == '2')){ //审核中
                $list['list'][$k]['merge_status'] = '2';
                $list['list'][$k]['merge_status_desc'] = '审核中';
            }

            if($v['custom_status'] == '4' || $v['invoice_status'] == '4'){ //驳回/待审核
                $list['list'][$k]['merge_status'] = '4';
                $list['list'][$k]['merge_status_desc'] = '已驳回';
            }

            if($v['custom_status'] == '3' && $v['invoice_status'] == '3'){ //通过
                $list['list'][$k]['merge_status'] = '3';
                $list['list'][$k]['merge_status_desc'] = '通过';
            }




        }
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function getsteelattrlist(Request $request)
    {
        $type_id = $request->type_id ?? 1;
        $status = $request->status ?? 0;
        $isgetall = $request->isgetall ?? 0;
        $cpid = intval($request->cpid) ?? 0;

        $steel_attr_type = SteelAttrType::where([['id', $type_id]])->whereIn('cpid', [0, $cpid])->first();
        if (!$steel_attr_type) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        $list = SteelAttrList::getListByCompany($request, $cpid, $type_id, $status, $isgetall);
        if ($type_id == 2) {
            $config_arr = config('steelconfig.steel_type_2');
            foreach ($list['list'] as &$val) {
                if (in_array($val['attr_val_name'], $config_arr)) {
                    $val['is_red'] = 2;
                } else {
                    $val['is_red'] = 1;
                }
            }
        }



        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    /***
     *  获取钢材规格
     *
     */
    public function getarchivespec(Request $request){
        //archive_spec
        $cpid = intval($request->cpid) ?? 0;

        $archive_spec_list = SteelArchive::where(['cpid' => $cpid])->select(['archive_spec'])->get();

        if($archive_spec_list->isEmpty()){
            $archive_spec_list = [];
        }else{
            $archive_spec_list = $archive_spec_list->toArray();
            $archive_spec_list = array_unique(array_column($archive_spec_list,'archive_spec'));
        }
        $archive_spec_list = array_filter($archive_spec_list,function($spec){
            if ($spec != '') return true;
        });
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $archive_spec_list]);
    }

    /**
     * 各端管理员列表
     */
    public function getmangerlist(Request $request)
    {
        $utype = $request->utype ?? 0;
        $status = $request->status ?? 0;
        $isgetall = $request->isgetall ?? 0;
        $cpid = intval($request->cpid) ?? 0;

        $list = MangerAccount::getMangerList($request, $cpid, $utype, $status, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }
}
